﻿using System;
using System.Text.RegularExpressions;

namespace XmlTagExtractor
{
    public class Program
    {
        public static void Main()
        {
            string text = "<name><first>Ivo</first></name><age>29</age>";
            text = Regex.Replace(text, @">\s+<", @"><");
            string tagName = @"<([^<>]+)>";
            Match tagNameMatch = Regex.Match(text, tagName);
            string pattern = @"<(" + tagNameMatch.Groups[1] + @")>((.|\s)+?)</" + tagNameMatch.Groups[1] + ">";

            Match match = Regex.Match(text, pattern);
            while (match.Success)
            {
                Console.WriteLine("tag={0}, value={1};", match.Groups[1],match.Groups[2]);
                text = Regex.Replace(text, pattern, @"$2");

                tagNameMatch = Regex.Match(text, tagName);
                pattern = @"<(" + tagNameMatch.Groups[1] + @")>((.|\s)+?)</" + tagNameMatch.Groups[1] + ">";
                match = Regex.Match(text, pattern);
            }
        }
    }
}
